package model.client.responsehandler;

import model.entity.User;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import javafx.stage.Stage;
import server.response.CreateGroupResponseMessage;
import model.util.ChannelUtils;
import view.ChatInterface;

import java.util.List;

/**
 * @Description 对服务器返回创建群聊回应消息的处理
 * @author czc
 */
public class CreateGroupResponseHandler extends SimpleChannelInboundHandler<CreateGroupResponseMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, CreateGroupResponseMessage msg) throws Exception {

        Channel channel = ctx.channel();
        List<String> username = msg.getUserNameList();
        User user = ChannelUtils.getChatInterface("1").getUser();
        String groupId = msg.getGroupId();
        ChatInterface chatInterface = new ChatInterface(user, new Stage());
        chatInterface.setChannel(channel);
        chatInterface.setInterfaceId(groupId);

        ChannelUtils.putChatInterface(groupId, chatInterface);
        //打开新建群聊窗口
        chatInterface.start();
        System.out.println("创建群聊成功, id为 [ " + groupId + "]");
        System.out.println("群里有 " + username);
    }
}
